package com.jeesite.modules.carousel.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.car.entity.Brand;
import com.jeesite.modules.car.entity.Car;
import com.jeesite.modules.car.entity.Image;
import com.jeesite.modules.carousel.entity.Carousel;
import com.jeesite.modules.carousel.service.CarouselService;
import com.jeesite.modules.examine.entity.Examine;
import com.jeesite.modules.general.CosUtil;
import com.jeesite.modules.merchant.entity.Merchant;
import com.jeesite.modules.sys.utils.RoleUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping(value = "${adminPath}/carousel/carousel")
public class CarouselController extends BaseController {

    @Autowired
    private CarouselService carouselService;

    @ModelAttribute
    public Carousel get(String carouselId, boolean isNewRecord) {
        return carouselService.get(carouselId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("carousel:carousel:view")
    @RequestMapping(value = {"list", ""})
    public String list(Carousel carousel, Model model) {
        model.addAttribute("carousel", carousel);
        model.addAttribute("baseUrl", CosUtil.URL);
        return "modules/carousel/carouselList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("carousel:carousel:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<Carousel> listData(Carousel carousel, HttpServletRequest request, HttpServletResponse response) {
        carousel.setPage(new Page<>(request, response));
        Page<Carousel> page = carouselService.findPage(carousel);
        return page;
    }

    /**
     * 表单编辑
     */
    @RequiresPermissions("carousel:carousel:edit")
    @RequestMapping(value = "form")
    public String formEdit(Carousel carousel, Model model) {
        model.addAttribute("carousel", carousel);
        model.addAttribute("baseUrl", CosUtil.URL);
        return "modules/carousel/carouselForm";
    }

    /**
     * 保存轮播图数据
     */
    @RequiresPermissions("carousel:carousel:edit")
    @PostMapping(value = "saveCarousel")
    @ResponseBody
    public String saveCarousel(@RequestParam(value = "files", required = false) MultipartFile[] files, @Validated Carousel carousel, String delCarousel) {
        //删除旧数据
        if(StringUtils.isNotEmpty(delCarousel)){
            CosUtil.delCOSFile(delCarousel, "carousel");
        }
        if (!Objects.isNull(files)) {
            for (MultipartFile file : files) {
                String[] name = (FilenameUtils.getName(file.getOriginalFilename())).split("_");
                if("carousel".equals(name[0])){
                    JSONObject result = CosUtil.uploadFile(file, "", "carousel");
                    if(!result.getBoolean("result")){
                        carousel.setCarouselUrl(result.getString("fileName2"));
                    }else {
                        return renderResult(Global.TRUE, text("轮播图片上传失败！"));
                    }
                }
            }
            if (carousel.getCarouselId() != null && !carousel.getCarouselId().equals("")){  // 修改
                carouselService.update(carousel);
            }else{   // 新增
                carouselService.save(carousel);
            }
        } else {   // 编辑情况  若图片未更改  走这里
            carouselService.update(carousel);
        }
        return renderResult(Global.TRUE, text("保存数据成功！"));
    }

    /**
     * 删除轮播图数据
     * @param carousel
     * @return
     */
    @RequiresPermissions("carousel:carousel:edit")
    @RequestMapping(value = "delCarouseInfo")
    @ResponseBody
    public String delCarouseInfo(Carousel carousel) {
        if (carousel.getCarouselId() != null && !carousel.getCarouselId().equals("")){
            carouselService.delCarouseData(carousel.getCarouselId());
            return renderResult(Global.TRUE, text("操作成功！"));
        }
        return renderResult(Global.FALSE, text("系统错误！"));
    }

}
